/**
 * 
 */
package com.evon.yardmanagement.export;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import com.evon.yardmanagement.model.Section;
import com.evon.yardmanagement.service.SectionManagementService;
import com.evon.yardmanagement.ui.dto.ActionDTO;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author Sandeep
 *
 */
public class AddImportToExcel extends ActionSupport{
	
	String fileName = "c:/test/test.xls";  
	List<Section> selectedActions = new ArrayList<Section>();
	
	  @SuppressWarnings({ "unchecked", "deprecation" })
	public String readExcelFile()

      {

          /** --Define a Vector

              --Holds Vectors Of Cells

           */

          //Vector cellVectorHolder = new Vector();

            List  cellVectorHolder = new ArrayList();

          

          /** Creating Input Stream**/

          //InputStream myInput= ReadExcelFile.class.getResourceAsStream( fileName );
         try{
          FileInputStream myInput = new FileInputStream(fileName);

 

          /** Create a POIFSFileSystem object**/

          POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);

 

          /** Create a workbook using the File System**/

           HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);

 

           /** Get the first sheet from workbook**/

          HSSFSheet mySheet = myWorkBook.getSheetAt(0);

          System.out.println("mySheet"+mySheet);

          /** We now need something to iterate through the cells.**/

            java.util.Iterator<HSSFRow> rowIter = mySheet.rowIterator();

 

            while(rowIter.hasNext()){

                HSSFRow myRow = (HSSFRow) rowIter.next();

                java.util.Iterator<HSSFCell> cellIter = myRow.cellIterator();

                //Vector cellStoreVector=new Vector();

                List cellStoreVector = new ArrayList();

                while(cellIter.hasNext()){

                    HSSFCell myCell = (HSSFCell) cellIter.next();

                   // cellStoreVector.addElement(myCell);

                    cellStoreVector.add(myCell);

                }

                //cellVectorHolder.addElement(cellStoreVector);

                cellVectorHolder.add(cellStoreVector);
                
                for (int i=1;i<cellVectorHolder.size();i++) {                 

                    List cellStoreVector1=(List)cellVectorHolder.get(i);

                        for (int j=0; j< cellStoreVector.size();j++){

                             HSSFCell myCell = (HSSFCell)cellStoreVector.get(j);
                                Section section = new Section();
                               
                                //	int a = Integer.valueOf((String) cellStoreVector.get(j));
                                	//int a = ((Integer) cellStoreVector.get(j)).intValue();
                                	  if(myCell.getCellNum() == 0){
                                    	  String a = cellStoreVector.get(j).toString();
                                    	  double aInt = Double.parseDouble(a);
                                    	  int y = (int)Math.round(aInt);
                                    	 // int a = ((Integer) cellStoreVector.get(j)).intValue();
                                    	System.out.println(y);
                                	section.setId(y);
                                	selectedActions.add(section);
                                  }
                                  if(myCell.getCellNum() == 1){
                                  	System.out.println(cellStoreVector.get(j));
                                    }
                              
                              //  selectedActions.add(section);
                                
                          }
                        
                }
                
                SectionManagementService sectionMS =  new SectionManagementService();
     //           sectionMS.save(selectedActions);


          

                }
            }
         catch(Exception e)
         {e.printStackTrace();}
         
         return SUCCESS;
         }
	  
}



